import numpy as np
import os
import cv2
import pandas as pd
from data_aug import resize
import skimage


label_dir = '../data/csv_label/cewei/train_cewei_box.csv'
save_path = '../data/show_new/box/cewei'
image_path = '../data/show_new/original/cewei'
# label_dir = '../data/csv_label_new/zhengwei/train_zhengwei_box.csv'
# save_path = '../data/show_new/box/zhengwei'
# image_path = '../data/show_new/original/zhengwei'
if not os.path.exists(save_path):
    os.makedirs(save_path)

data = pd.read_csv(label_dir)


def draw_caption(image, box, caption):
    b = np.array(box).astype(int)
    cv2.putText(image, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 2)
    cv2.putText(image, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)


if __name__ == '__main__':

    image_id = data.iloc[0,0]
    ## 读取图片然后画上box
    # image_dir = os.path.join('../data/data/jpg_file', image_id.split('/')[-1].replace('npz', 'png'))
    ## 读取加上point的可视化图片，画上box
    image_dir = os.path.join(image_path,image_id.split('/')[-1].replace('npz','png'))
    image = cv2.imread(image_dir)
    for index in range(len(data)):
        print('--------------')
        if data.iloc[index,0] != image_id:
            image = cv2.resize(image,(640,1024))

            # cv2.imshow('img', image)
            # cv2.waitKey(0)
            cv2.imwrite(os.path.join(save_path,image_id.split('/')[-1].replace('npz', 'png')),image)

            image_id = data.iloc[index,0]
            image_dir = os.path.join(image_path, image_id.split('/')[-1].replace('npz', 'png'))
            image = cv2.imread(image_dir)


        x1 = data.iloc[index,1]
        y1 = data.iloc[index,2]
        x2 = data.iloc[index,3]
        y2 = data.iloc[index,4]
        label_name = data.iloc[index,5]
        box = [x1,y1,x2,y2]
        box = [box]
        print('image shape',image.shape)
        # resize_image,box,label_name = resize(image,bboxes=box,category_id=label_name)
        # print('resized image shape', image.shape)
        box = box[0]
        box = [int(i) for i in box]
        # draw_caption(image, box, label_name)
        cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), color=(0, 255, 255), thickness=5)


